<?php
session_start();
include("includes/inc_funcs.php");
date_default_timezone_set('GMT');
$MAX_FILE_SIZE = 4000000;
?>
<html>
<head>
  <script type="text/JavaScript" src="/js/jquery-1.9.0.js"></script>
<style type="text/css">
table.events {
  float: left;
}
table.events td {
  border: solid #000 1px;
}
table.events th {
  border: solid #000 1px;
}
div.events {
  float: left;
}

</style>
<script language="JavaScript">
<!--
function toggleVisible(id) {
   var a = document.getElementById?document.getElementById(id):document.all[id];
   if (a) {
   if (a.style.display == 'block') a.style.display = 'none';
   else a.style.display = 'block';
   }
   return false;
}
//-->
</script>

<link rel="stylesheet" href="/css/styles.css" type="text/css" />
</head>
<body>
<div id="top_bar">
  <div id="Login"><a href="login.php">Login</a></div>
  <div id="Register">Register</div>
  <div id="contact">Contact Us</div>
</div>
<div id="top">
  <form enctype="multipart/form-data" action="index.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $MAX_FILE_SIZE;?>" />
    <!--<input name="userfile" type="file" /><br />
    <label for="title">Title:</label><input type="text" name="title" /><br />-->
    <div id="search1">Search</div>
    <div id="upfile1">Upload</div>
    <input type="file" id="file1" name="userfile" style="display:none" />
    <!--<label for="keywords">Keywords:</label><input type="text" name="keywords" /><br />
    <label for="description">Decsription:</label><input type="text" name="description" /><br />
    <input type="submit" value="Upload File" style="position:absolute;"/>-->
  </form>
  <form action="" method="POST">
    <div id="search">
      <input type="text" />
    </div>
  </form>
</div>
<br /><br /><br />
<script type="text/javascript">
$("#upfile1").click(function () {
  $("#file1").trigger('click');
});
</script>
<div id="container">
<?php
  echo "<div id=\"advert_box_x\">ADVERTISEMENT<br /><br />ADVERTISEMENT<br /><br />ADVERTISEMENT</div>";
  echo "<div class=\"content\">";
  echo "<table cellpadding=\"0\" style=\"width:100%;\">
    <tr><td style=\"width:100%;\">";
  echo showreplays(10,0);
  echo showreplays(10,1);
  echo showreplays(10,2);
  echo "</td><td><div id=\"advert_box_y\">
  <br /><br /><br /><br />ADVERTISEMENT<br /><br /><br /><br /><br />ADVERTISEMENT
  <br /><br /><br /><br /><br />ADVERTISEMENT<br /><br /><br /><br /><br />ADVERTISEMENT
  <br /><br /><br /><br /><br />ADVERTISEMENT<br /><br /><br /><br /><br />ADVERTISEMENT
  <br /><br /><br /><br /><br />ADVERTISEMENT<br /><br /><br /><br /><br />ADVERTISEMENT
  </div></td></tr></table>";
  echo"</div>";
if (isset($_FILES['userfile'])) {
  $error = $_FILES['userfile']['error'];
  $type = $_FILES['userfile']['type'];
  $name = $_FILES['userfile']['name'];
  $tmpname = $_FILES['userfile']['tmp_name'];
  $size = $_FILES['userfile']['size'];
  $err = false;
  if ($size >= $MAX_FILE_SIZE) {
    echo "Error: The uploaded file was too large. The maximum size is ".$MAX_FILE_SIZE." bytes.<br />";
    $err = true;
  }
  if ($error == UPLOAD_ERR_PARTIAL) {
    echo "Error: The upload was not completed successfully. Please try again.<br />";
    $err = true;
  }
  if ($error == UPLOAD_ERR_NO_FILE) {
    echo "Error: No file was selected for uploading.<br />";
    $err = true;
  }
  if (!is_uploaded_file($tmpname)) {
    echo "Error: Uploaded filename doesn't point to an uploaded file.<br />";
    $err = true;
  }
  if ($err !== true) {
    if ($_POST['debug'] == 1) {
      error_reporting(-1);
    }
    if (class_exists("MPQFile") || (include 'mpqfile.php')) {
      $start = microtime_float();
      $parseDurationString = "";
      $debug = 0;
      if ($_POST['debug'] == 1 || $_POST['test'] == 1) {
        echo sprintf("<b>Debugging is on.</b><br />\n");
        $debug = 2;
      }
      $a = new MPQFile($tmpname,true,$debug);
      $init = $a->getState();
      if ($init == false)
        echo "Error parsing uploaded file, make sure it is a valid MPQ archive!<br />\n";
      else if ($a->getFileType() == "SC2replay") {
        $b = $a->parseReplay();
        $player_numbers = $b->getRealTeamSize();        
        $map = $b->getMapName();
        $length = $b->getFormattedGameLength();
        $speed = $b->getGameSpeedText();
        $mode = $b->getTeamSize();
        $realm = $b->getRealm();
        $date = $b->getCtime();
        $version = $a->getVersionString();

        $players = $b->getPlayers();
        $names='';
        $races='';
        $colours='';
        foreach($players as $player) {
          $player_names.=$player['name']."|";
          $player_races.=$player['lrace']."|";
          $player_colours.=$player['color']."|";
          print_r($player);
          echo "<br/ ><br />";
        }
        print_r($player_names);
        echo "<br />";
        print_r($player_races);
        echo "<br />";
        print_r($player_colours);
        echo "<br />";

        savereplay($title,$keywords,$description,$filename,$player_numbers,$player_names,$player_races,$player_colours,$map,$length,$speed,$mode,$realm,$winner,$version,$date_played);

        /*echo sprintf("Version: %s<br />\n",$version);
        $parseDurationString .= sprintf("Parsed replay in %d ms.<br />\n",((microtime_float() - $start)*1000));
        $players = $b->getPlayers();
        $recorder = $b->getRecorder();
        echo sprintf("Map name: %s, Game length: %s<br />\n",$map,$length);
        echo sprintf("Team size: %s, Game speed: %s<br />\n",$mode, $speed);
        echo sprintf("Real team size: %s<br />\n",$player_numbers);
        echo sprintf("Realm: %s<br />\n",$realm);
        echo sprintf("Date and time played: %s<br />\n",date('jS \of F Y \a\t H:i' ,$date));
        if ($recorder != null)
          echo sprintf("Replay recorded by: %s <br />\n",$recorder['name']);
        //$apmString = "<b>APM graphs</b><br />\n";
        echo "<br />";
        $obsString = "";
        $obsCount = 0;
        echo "<div id=\"right\"><table border=\"1\"><tr><th>Player name</th><th>Race</th><th>Color</th><th>Team</th><th>Average APM<br />(experimental)</th><th>Winner?</th></tr>\n";
        foreach($players as $value) {
          if ($value['isObs']) {
            if ($obsString == "")
              $obsString = $value['name'];
            else
              $obsString .= ', '.$value['name'];
            $obsCount++;
            continue;
          }
          if ($b->isWinnerKnown())
            $wincolor = (isset($value['won']) && $value['won'] == 1)?0x00FF00:0xFF0000;
          else
            $wincolor = 0xFFFFFF;
          if ($value['isComp'] && $b->getTeamSize() !== null)
            $difficultyString = sprintf(" (%s)",SC2Replay::$difficultyLevels[$value['difficulty']]);
          else
            $difficultyString = "";
          echo sprintf("<tr><td>%s</td><td>%s</td><td><font color=\"#%s\">%s</font></td><td>%s</td><td style=\"text-align: center\">%d</td><td style=\"background-color: #%06X; text-align: center\">%d</td></tr>\n",
                  $value['name'].$difficultyString,
                  $value['lrace'],
                  $value['color'],
                  $value['sColor'],
                  ($value['team'] > 0)?"Team ".$value['team']:"-",
                  ($value['team'] > 0)?(round($value['apmtotal'] / ($b->getGameLength() / 60))):0,
                  ((isset($value['won']))?$wincolor:0xFFFFFF),
                  (isset($value['won']))?$value['won']:(($value['team'] > 0)?"Unknown":"-")
                );
        }
        echo "</table></div><br />";
        if ($obsCount > 0) {
          echo "Observers ($obsCount): $obsString<br />\n";
        }*/
      }
    }
  }
}
?>

</div>
</body>
</html>
